//
//  ViewController.swift
//  Location
//
//  Created by xmg on 16/8/21.
//  Copyright © 2016年 wlis. All rights reserved.
//

import UIKit
import CoreLocation
class ViewController: UIViewController {

    private lazy var manager :CLLocationManager  = CLLocationManager()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        manager.requestWhenInUseAuthorization()
        
        manager.startUpdatingLocation()
        manager.delegate = self
        // 1.设置定位的精确度
        /*
        kCLLocationAccuracyBestForNavigation: 导航精确度(最精确)
        kCLLocationAccuracyBest: 最好精确度(默认)
        kCLLocationAccuracyNearestTenMeters: 10米的误差
        kCLLocationAccuracyHundredMeters: 100米的误差
        kCLLocationAccuracyKilometer: 千米误差
        kCLLocationAccuracyThreeKilometers: 三千米的误差
        */
        manager.desiredAccuracy = kCLLocationAccuracyBest
        /** 设置移动距离 */
        manager.distanceFilter = 100
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

extension ViewController : CLLocationManagerDelegate{

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        /** 定义用户的位置 */
        manager.stopUpdatingLocation()
        
        /** 校验用户是否有位置 */
        guard let lastLocation = locations.last else{return}
        /** 获取具体的信息 */
        /** 获取用户的经纬度 */
        let coordinate = lastLocation.coordinate
        print("纬度:\(coordinate.latitude) 经度:\(coordinate.longitude)")
        /** 获取用户所在的海拔 */
        let altitude = lastLocation.altitude
        print("海拔:\(altitude)")
        /** 水平精度和垂直精度 */
        let hAccuracy = lastLocation.horizontalAccuracy
        let vAccuracy = lastLocation.verticalAccuracy
        print("水平精确度:\(hAccuracy) 垂直精确度:\(vAccuracy)")
        /** 当前的运动方向 */
        let course = lastLocation.course
        print("运动方向:\(course)")
        /** 获取当前的速度 */
        let speed = lastLocation.speed
        print("速度:\(speed)")
        
    }
    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        switch status {
        case .AuthorizedAlways:
            print("总是授权")
        case .AuthorizedWhenInUse:
            print("前台授权")
        case .Denied:
            print("拒绝访问")
        case .NotDetermined:
            print("没有决定")
        case .Restricted: // 由于系统原因不允许访问用户的问题
            print("系统原因")
        }
    }
}